﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class FileDeleteActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property Target As InArgument(Of String)
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncFileDeleteDelegate(ByVal target As String) As Boolean

    Public Sub New()
        Me.DisplayName = My.Resources.FileDeleteDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult
        Dim target = context.GetValue(Me.Target)
        Dim asyncExecute = New asyncFileDeleteDelegate(AddressOf FileDelete)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(target, callback, state)
    End Function

    Protected Overrides Sub EndExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncFileDeleteDelegate)
        Dim copyResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, copyResult)
    End Sub

    Private Function FileDelete(ByVal target As String) As Boolean
        Dim deleteResult = False
        Try
            My.Computer.FileSystem.DeleteFile(target)
            deleteResult = True
        Catch ex As Exception

        End Try
        Return deleteResult
    End Function

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder

        'FileDelete Activity
        build.AddCustomAttributes(GetType(FileDeleteActivity),
                                              New ToolboxBitmapAttribute(GetType(FileDeleteActivity), "FileDelete.png"))
        build.AddCustomAttributes(GetType(FileDeleteActivity), "Target",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FilepathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FileDeleteCategory),
                                              New DisplayNameAttribute(My.Resources.FileDeleteTargetDisplay),
                                              New DescriptionAttribute(My.Resources.FileDeleteTargetDescription))
        build.AddCustomAttributes(GetType(FileDeleteActivity), "Result",
                                              New CategoryAttribute(My.Resources.FileDeleteCategory),
                                              New DisplayNameAttribute(My.Resources.FileDeleteResultDisplay),
                                              New DescriptionAttribute(My.Resources.FileDeleteResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub
End Class
